﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Dict.Config;

namespace UI
{
    
    public class BaseAttributeData : IEnumerable<KeyValuePair<string, double>>
    {
        private const int AllAttributeCount = 11;
        public double[] attributes;

        private static BaseAttributeData ZERO = new BaseAttributeData(0);


        public static BaseAttributeData zero
        {
            get
            {
                return ZERO;
            }
        }

        public BaseAttributeData()
        {
            attributes = new double[AllAttributeCount];
        }

        public BaseAttributeData(BaseAttributeData data)
        {
            attributes = new double[AllAttributeCount];
            for (int i = 0; i < AllAttributeCount; i++)
            {
                this.attributes[i] = data.attributes[i];
            }
        }


        public BaseAttributeData(double factor)
        {
            attributes = new double[AllAttributeCount];
            for (int i = 0; i < AllAttributeCount; i++)
            {
                attributes[i] = factor;
            }
        }

        public BaseAttributeData(ProtoBuf.Message.BaseProp baseProp)
        {
            attributes = new double[AllAttributeCount];
            SetProp(AttributeQueue.Attack, baseProp.att);
            SetProp(AttributeQueue.Hp, baseProp.hp);
            SetProp(AttributeQueue.PhyDefend, baseProp.pdef);
            SetProp(AttributeQueue.MagicDefend, baseProp.mdef);
            SetProp(AttributeQueue.hit, baseProp.hit);
            SetProp(AttributeQueue.dodge, baseProp.dodge);
            SetProp(AttributeQueue.cri, baseProp.cri);
            SetProp(AttributeQueue.antiCri, baseProp.antiCri);
            SetProp(AttributeQueue.criFactor, baseProp.criFactor);
            SetProp(AttributeQueue.damageAdd, baseProp.damageAdd);
            SetProp(AttributeQueue.damageReduce, baseProp.damageReduce);
        }
        #region 改

        public void SetProp(AttributeQueue propId, double value)
        {
            attributes[(int)propId] = value;
        }

        public void SetProp(string propName,double value)
        {
            attributes[GetIndexByDictPropId(propName)] = value;
        }

        public void AddProp(AttributeQueue propId, double value)
        {
            attributes[(int)propId] += value;
        }

        public void AddProp(string propId,double value)
        {
            attributes[GetIndexByDictPropId(propId)] += value;
        }

       

        public void Reset()
        {
            for (int i = 0; i < AllAttributeCount; i++)
            {
                attributes[i] = 0;
            }
        }
        #endregion

        #region 查
        public double GetProp(string propId)
        {
            int index = GetIndexByDictPropId(propId);
            return attributes[index];
        }

        public double GetProp(AttributeQueue propId)
        {
            return attributes[(int)propId];
        }

        public bool IsZero()
        {
            for (int i = 0; i < AllAttributeCount; i++)
            {
                if (attributes[i] != 0)
                    return false;
            }
            return true;
        }
        
        #endregion

        public static int GetIndexByDictPropId(string porpId)
        {
            switch (porpId)
            {
                case TableConst.Hp:
                    return 0;
                case TableConst.Attack:
                    return 1;
                case TableConst.PhyDefend:
                    return 2;
                case TableConst.MagicDefend:
                    return 3;
                case TableConst.hit:
                    return 4;
                case TableConst.dodge:
                    return 5;
                case TableConst.cri:
                    return 6;
                case TableConst.antiCri:
                    return 7;
                case TableConst.criFactor:
                    return 8;
                case TableConst.damageAdd:
                    return 9;
                case TableConst.damageReduce:
                    return 10;
                
                //add other attribute process;
            }
            return 0;
        }

        public static string GetDictPropIdByIndex(int index)
        {
            switch (index)
            {
                case 0:
                    return TableConst.Hp;
                case 1:
                    return TableConst.Attack;
                case 2:
                    return TableConst.PhyDefend;
                case 3:
                    return TableConst.MagicDefend;
                case 4:
                    return TableConst.hit;
                case 5:
                    return TableConst.dodge;
                case 6:
                    return TableConst.cri;
                case 7:
                    return TableConst.antiCri;
                case 8:
                    return TableConst.criFactor;
                case 9:
                    return TableConst.damageAdd;
                case 10:
                    return TableConst.damageReduce;
                
                //add other attribute process;
            }
            return "default";
        }




        public override string ToString()
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            for (int i = 0; i < AllAttributeCount; i++)
            {
                sb.Append(GetDictPropIdByIndex(i).ToString() + ":" + this.attributes[i].ToString() + " ");
            }
            return sb.ToString();
        }


        public BaseAttributeData Multiply(double d)
        {
            for (int i = 0; i < AllAttributeCount; i++)
            {
                attributes[i] *= d;
            }
            return this;
        }

        public BaseAttributeData Multiply(BaseAttributeData d)
        {
            for (int i = 0; i < AllAttributeCount; i++)
            {
                attributes[i] *= d.attributes[i];
            }
            return this;
        }

        public BaseAttributeData AddProp(BaseAttributeData d)
        {
            for (int i = 0; i < AllAttributeCount; i++)
            {
                attributes[i] += d.attributes[i];
            }
            return this;
        }

        public BaseAttributeData SubProp(BaseAttributeData d)
        {
            for (int i = 0; i < AllAttributeCount; i++)
            {
                attributes[i] -= d.attributes[i];
            }
            return this;
        }

        public BaseAttributeData AddProp(double d)
        {
            for (int i = 0; i < AllAttributeCount; i++)
            {
                attributes[i] += d;
            }
            return this;
        }

        IEnumerator<KeyValuePair<string, double>> IEnumerable<KeyValuePair<string, double>>.GetEnumerator()
        {
            for (int i = 1; i < AllAttributeCount; i++)
            {
                KeyValuePair<string, double> d = new KeyValuePair<string, double>(GetDictPropIdByIndex(i), attributes[i]);
                yield return d;
            }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            for (int i = 1; i < AllAttributeCount; i++)
            {
                KeyValuePair<string, double> d = new KeyValuePair<string, double>(GetDictPropIdByIndex(i), attributes[i]);
                yield return d;
            }
        }
    }
}